//
//  RegisterViewController.m
//  个人项目(豆瓣元旦作业)
//
//  Created by dllo on 16/1/4.
//  Copyright © 2016年 guoguo. All rights reserved.
//

#import "RegisterViewController.h"
#import "DengLuDataBaseModel.h"
#import "HGdlDataBase.h"
#import "DengLuViewController.h"
@interface RegisterViewController ()

@property (nonatomic, retain) UITextField *text1;
@property (nonatomic, retain) UITextField *text2;
@property (nonatomic, retain) UITextField *text3;
@property (nonatomic ,retain) UITextField *text4;
@property (nonatomic, retain) UITextField *text5;


@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"注册";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *label1 =[[UILabel alloc] initWithFrame:CGRectMake(30, 70, 100, 50)];
    label1.text = @"用户名:";
    label1.font = [UIFont systemFontOfSize:21];
    
    [self.view addSubview:label1];
    
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(30, 120, 100, 50)];
    label2.text = @"密码:";
    label2.font = [UIFont systemFontOfSize:21];
    [self.view addSubview:label2];
    
    UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(30, 170, 100, 50)];
    label3.text = @"确认密码:";
    label3.font = [UIFont systemFontOfSize:21];
    [self.view addSubview:label3];
    
    UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(30, 220, 100, 50)];
    label4.text = @"邮箱:";
    label4.font = [UIFont systemFontOfSize:21];
    [self.view addSubview:label4];
    
    UILabel *label5 = [[UILabel alloc] initWithFrame:CGRectMake(30, 270, 100, 50)];
    label5.text = @"联系方式:";
    label5.font = [UIFont systemFontOfSize:21];
    [self.view addSubview:label5];
    
    
    self.text1 = [[UITextField alloc] initWithFrame:CGRectMake(140, 80, 200, 30)];
    _text1.placeholder = @"请输入用户名";
    _text1.clearButtonMode = YES;
    _text1.clearsOnBeginEditing = YES;
    _text1.borderStyle = UITextBorderStyleRoundedRect;
    
    [self.view addSubview:_text1];
    
    self.text2 = [[UITextField alloc] initWithFrame:CGRectMake(140, 130, 200, 30)];
    _text2.placeholder = @"请输入密码";
    _text2.clearButtonMode = YES;
    _text2.clearsOnBeginEditing = YES;
    _text2.borderStyle = UITextBorderStyleRoundedRect;
    _text2.secureTextEntry = YES;
    [self.view addSubview:_text2];
    
    
   self.text3 = [[UITextField alloc] initWithFrame:CGRectMake(140, 180, 200, 30)];
    _text3.placeholder = @"请输入用户名";
    _text3.clearButtonMode = YES;
    _text3.clearsOnBeginEditing = YES;
    _text3.borderStyle = UITextBorderStyleRoundedRect;
    _text3.secureTextEntry = YES;
    
    [self.view addSubview:_text3];
    
    self.text4 = [[UITextField alloc] initWithFrame:CGRectMake(140, 230, 200, 30)];
    _text4.placeholder = @"请输入邮箱";
    _text4.clearButtonMode = YES;
    _text4.clearsOnBeginEditing = YES;
    _text4.borderStyle = UITextBorderStyleRoundedRect;
    
    [self.view addSubview:_text4];
    
    
    self.text5 = [[UITextField alloc] initWithFrame:CGRectMake(140, 280, 200, 30)];
    _text5.placeholder = @"请输入联系方式";
    _text5.clearButtonMode = YES;
    _text5.clearsOnBeginEditing = YES;
    _text5.borderStyle = UITextBorderStyleRoundedRect;
    
    [self.view addSubview:_text5];
    
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    
    self.navigationItem.rightBarButtonItem = item;
}

//注册按钮
- (void)itemAction:(UIBarButtonItem *)item {
    
    NSMutableArray *arr = [[HGdlDataBase sharedHGdlDataBase] selectData];
    
    NSMutableArray *strM = [NSMutableArray array];
    
    for (DengLuDataBaseModel *model in arr) {
        NSString *str = model.textFieldM;
        [strM addObject:str];
    }
    
    
    if ([strM containsObject:_text1.text]) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"该用户名已被注册" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"重新输入" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [_text1 becomeFirstResponder];
            _text2.text = nil;
            _text3.text = nil;
            _text4.text = nil;
            _text5.text = nil;
            _text1.text = nil;
        }];
        
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
    }else{
        
        //插入数据
        self.model = [DengLuDataBaseModel dengLuDataBaseModel];
        _model.textFieldM = _text1.text;
        _model.textFieldS = _text2.text;
        _model.textFieldY = _text4.text;
        _model.textFieldC = _text5.text;
        _model.a = 1;
        [[HGdlDataBase sharedHGdlDataBase] insertData:_model];
        
        
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注册成功" message:@"请登录" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
            [[HGdlDataBase sharedHGdlDataBase] deleteData:_model];
            
            
        }];
        
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"登陆" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }];
        
        
        [alert addAction:action];
        
        [alert addAction:action1];
        
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
    }
    
    
    
    
    
    
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
